package com.example.util;

import android.content.Context;
import android.content.SharedPreferences;


public class SharedPreferencesDB {
	private SharedPreferences dataStorage;
	private static final String FILENAME = "buchmeo_data";
	private static final String ID = "id";
	private static final String ID_CONTACT = "id_contact";
	private static final String ID_DEVICE = "id_device";
	private static final String TOKEN = "token";
	private static final String EMAIL = "email";
	private static final String FIRSTNAME = "firstname";
	private static final String LASTNAME = "lastname";
	private static final String PHONE = "phone";
	private static final String ADDRESS = "address";
	private static final String PASSWORD = "password";
	private static final String IS_UPDATED  = "updated";
	private static final boolean FIRST_START  =  false;

	public SharedPreferencesDB(Context context) {
		dataStorage = context.getSharedPreferences(FILENAME,
				Context.MODE_PRIVATE);
	}

	public void clearPreferences() {
       dataStorage.edit().clear();
	}
	public void setPrefToken(String value) {
		setPreferenceValue(TOKEN, value);
	}
	public void setPrefIdDevice(String value) {
		setPreferenceValue(ID_DEVICE, value);
	}
	public void setPrefIdContact(String value) {
		setPreferenceValue(ID_CONTACT, value);
	}
	public void setPrefFirstName(String value) {
		setPreferenceValue(FIRSTNAME, value);
	}
	public void setPrefLastName(String value) {
		setPreferenceValue(LASTNAME, value);
	}
	public void setPrefAddress(String value) {
		setPreferenceValue(ADDRESS, value);
	}
	public void setPrefPhone(String value) {
		setPreferenceValue(PHONE, value);
	}
	public void setPrefEmail(String value) {
		setPreferenceValue(EMAIL, value);
	}
	public void setPrefId(String value) {
		setPreferenceValue(ID, value);
	}

	public void setPrefPassword(String value) {
		setPreferenceValue(PASSWORD, value);
	}
	public void setPrefUpdateId(Boolean value) {
		setPreferenceBooleanValue(IS_UPDATED, value);
	}
	public String getPrefToken() {
		return getPreferenceValue(TOKEN);
	}
	public String getPrefId() {
		return getPreferenceValue(ID);
	}
	public String getPrefIdContact() {
		return getPreferenceValue(ID_CONTACT);
	}
	public String getPrefFirstName() {
		return getPreferenceValue(FIRSTNAME);
	}
	public String getPrefLastName() {
		return getPreferenceValue(LASTNAME);
	}
	public String getPrefAddress() {
		return getPreferenceValue(ADDRESS);
	}
	public String getPrefPhone() {
		return getPreferenceValue(PHONE);
	}
	public String getPrefIdDevice() {
		return getPreferenceValue(ID_DEVICE);
	}

	public String getPrefEmail() {
		return getPreferenceValue(EMAIL);
	}

	public String getPrefPassword() {
		return getPreferenceValue(PASSWORD);
	}
	public void setPrefFirstStart(boolean is){
		setPreferenceBooleanValue(FIRSTNAME, is);
	}
	public boolean getPrefFirstStart(){
		return getPreferenceBooleanValue(FIRSTNAME);
	}
	public boolean getPrefIsUpdateId(){
		return getPreferenceBooleanValue(IS_UPDATED);
	}
	

	
	
	
	private String getPreferenceValue(String key) {
		String value = "";
		try {
			value = dataStorage.getString(key, Constant.BLANK);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return value;
	}

	private boolean getPreferenceBooleanValue(String key) {
		boolean value = false;
		try {
			value = dataStorage.getBoolean(key, false);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return value;
	}

	private void setPreferenceValue(String key, String value) {
		try {
			SharedPreferences.Editor editor = dataStorage.edit();
			editor.putString(key, value);
			editor.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void setPreferenceBooleanValue(String key, boolean value) {
		try {
			SharedPreferences.Editor editor = dataStorage.edit();
			editor.putBoolean(key, value);
			editor.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public String toString() {
		return "SharedPreferencesDB [id=" + getPrefId() + ", id_device=" + getPrefIdDevice() + 
				", id_contact=" + getPrefIdContact() + ", token=" + getPrefToken() + ", firstname=" +
				getPrefFirstName() + ", lastname=" + getPrefLastName() + ", phone=" + getPrefPhone() + ", email=" + 
				getPrefEmail() + ", address=" + getPrefAddress()+ "]";
	}
 

}
